package com.itheima.springbootwebbtalis.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.springbootwebbtalis.mapper.StudentMapper;
import com.itheima.springbootwebbtalis.pojo.PageResult;
import com.itheima.springbootwebbtalis.pojo.Student;
import com.itheima.springbootwebbtalis.pojo.StudentQueryParam;
import com.itheima.springbootwebbtalis.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult page(StudentQueryParam param) {
        PageHelper.startPage(param.getPage(),param.getPageSize());
        Page<Student> page=studentMapper.selectByPage(param);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.add(student);
    }

    @Override
    public Student showById(Integer id) {
        Student student=studentMapper.showById(id);
        return student;
    }

    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    @Override
    public void deleteById(List<Integer> ids) {
        studentMapper.deleteById(ids);
    }

    @Override
    public void violation(Integer id, short score) {
        studentMapper.violation(id,score);
    }
}
